public class mergeSort {


    public static void mergeSort(int[] elem){
        mergeSortChild(elem,0,elem.length-1);
    }
    public static void mergeSortChild(int[] elem,int left,int right){
        if (left >= right){
            return;
        }

        int mid = (right + left)/2;

        mergeSortChild(elem,left,mid);

        mergeSortChild(elem,mid +1,right);

        merge(elem,left,mid,right);
    }
    public static void merge(int[] elem,int left,int mid,int right){
        int[] tem = new int[right - left + 1];
        int i = 0;
        int s1 = left;
        int e1 = mid;
        int s2 = mid+1;
        int e2 = right;
        while (s1 <= e1 && s2 <= e2){
            if (elem[s1] <= elem[s2]){
                tem[i++] = elem[s1++];
            }else {
                tem[i++] = elem[s2++];
            }
        }
        while (s1 <= e1){
            tem[i++] = elem[s1++];
        }
        while (s2 <= e2){
            tem[i++] = elem[s2++];
        }
        for (int j = 0; j < tem.length; j++) {
            elem[left + j] = tem[j];
        }
    }
}
